/*
 ----------------------------------------------------------------------------------
                           "THE BEER-WARE COPYRIGHT LICENSE"
                                     (Revision 43)
                                      -----------
    Rymek [rymek@rymek.org] wrote this file. As long as You retain this notice and
    this file has been sent directly to You by the author, You can do whatever You
    want with this stuff as long as You don't sell or rent it. If we meet some day,
    and You think this stuff is worth it, You can buy me a beer in return :-)
                                                                 __________
                                                                 Paweł Rymaszewski
  ----------------------------------------------------------------------------------
 */

package processing.logic;

import constants.QueryConstants;
import processing.logic.util.Connector;
import processing.logic.util.Mapper;
import processing.objects.*;

import java.sql.SQLException;
import java.text.ParseException;
import java.util.TreeMap;

/**
 * Class containing mechanisms for searching for messages matching specific criteria
 *
 * @author rymek
 *         User: rymek
 *         Date: 30.06.11
 *         Time: 10:10
 */
public class Search extends Connector {

    /**
     * Constructor.
     * Also creates Datbase connection.
     *
     * @param server server information
     * @throws SQLException thrown when creating database connection fails
     */
    public Search(Server server) throws SQLException {
        super(server);
        super.createConnection();
    }

    /**
     * Finds messages matching search criteria
     *
     * @param searchParameters search criteria
     * @return SearchResults search results
     * @throws SQLException   thrown when database error occurs
     * @throws ParseException thrown when parsing error occurs
     */
    public Results findMessages(SearchParameters searchParameters)
            throws SQLException, ParseException {

        super.createConnection();

        Results results = new Results(searchParameters);

        super.preparedStatement = Mapper.buildPreparedSearchQuery(searchParameters, this.connection);
        super.resultSet = preparedStatement.executeQuery();

        // fetch message headers matching search criteria
        while (super.resultSet.next()) {

            Result result = new Result();

            result.setId(super.resultSet.getString(1));
            result.setMsgRcvDttm(super.resultSet.getTimestamp(2));
            result.setMsgRspDttm(super.resultSet.getTimestamp(3));
            result.setProcessingTime(super.resultSet.getInt(4));
            result.setStatus(super.resultSet.getString(5));

            results.addRequestResult(result);
        }

        // closing the db connection and all open db objects
        super.cleanup();

        return results;
    }

    /**
     * Method finds all available messages on specifid database
     *
     * @return - all available messages for input database
     * @throws SQLException - thrown when DB error occurs
     */
    public TreeMap<String, Message> getAllMessages() throws SQLException {

        super.createConnection();

        TreeMap<String, Message> messages = new TreeMap<>();

        // fetching non-custom publisher messages
        super.preparedStatement = this.connection.prepareStatement(QueryConstants.FETCH_AVAILABLE_MESSAGES);
        super.resultSet = preparedStatement.executeQuery();

        while (super.resultSet.next()) {

            Message message = new Message();

            message.setRequestName(super.resultSet.getString(1));
            message.setResponseName(super.resultSet.getString(2));
            message.setTransactionType(super.resultSet.getString(3));
            message.setEffectiveDate(super.resultSet.getDate(4).toString());
            message.setChannelName(super.resultSet.getString(5));
            message.setNodeName(super.resultSet.getString(6));
            message.setServiceName(super.resultSet.getString(7));
            message.setActive(super.resultSet.getString(8));
            message.setCustomPublisher(false);

            messages.put(message.getRequestName(), message);
        }

        // fetch custom publisher messages
        super.preparedStatement = this.connection.prepareStatement(QueryConstants.CPUBLISHER_FETCH_AVAILABLE_MESSAGES);
        super.resultSet = preparedStatement.executeQuery();

        while (super.resultSet.next()) {
            Message message = messages.get(super.resultSet.getString(1));
            if (message != null) {
                message.setCustomPublisher(true);
                message.setServerNbr(super.resultSet.getInt(2));
            }
        }

        // closing the db connection and all open db objects
        super.cleanup();

        return messages;
    }
}